package ar.com.personalTravellers.services.impl;

import ar.com.personalTravellers.beans.Client;
import ar.com.personalTravellers.beans.ClientConsultation;
import ar.com.personalTravellers.beans.Country;
import ar.com.personalTravellers.daos.ClientConsultationDAO;
import ar.com.personalTravellers.dtos.ClientConsultationDTO;
import ar.com.personalTravellers.services.ClientConsultationService;
import ar.com.personalTravellers.services.ClientService;
import ar.com.personalTravellers.services.CountryService;
import com.angel.arquitectura.model.common.PersistentObject;
import com.angel.arquitectura.service.impl.GenericServiceImpl;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Collection;

public class ClientConsultationServiceImpl extends GenericServiceImpl implements ClientConsultationService {

    private static Logger LOGGER = Logger.getLogger(ClientConsultationServiceImpl.class);

    @Autowired
    private ClientService clientService;
    @Autowired
    private CountryService countryService;

    protected ClientConsultationDAO getClientConsultationDAO(){
        return (ClientConsultationDAO) super.getGenericDAO();
    }

    public Collection<ClientConsultation> findAllByClientNameAndSurname(String clientName, String clientSurname) {
        Client client = this.getClientService().findUniqueByNameAndSurname(clientName, clientSurname);
        return getClientConsultationDAO().findAllByClient(client);
    }

    public PersistentObject createClientConsultation(ClientConsultationDTO clientConsultationDTO) {
        String clientName = clientConsultationDTO.getClientName();
        String clientSurname = clientConsultationDTO.getClientSurname();
        String clientEmail = clientConsultationDTO.getClientEmail();

        Country country = this.getCountryService().findUniqueByName(clientConsultationDTO.getClientCountry());
        Client client = this.getClientService().findUniqueOrNullByNameSurnameAndEmail(clientName, clientSurname, clientEmail);

        if(client == null){
            client = clientConsultationDTO.buildClient(country);
            this.getClientService().create(client);
        }
        ClientConsultation clientConsultation = clientConsultationDTO.buildClientConsultation(client);
        super.create(clientConsultation);
        return clientConsultation;
    }

    public ClientService getClientService() {
        return clientService;
    }

    public void setClientService(ClientService clientService) {
        this.clientService = clientService;
    }

    public CountryService getCountryService() {
        return countryService;
    }

    public void setCountryService(CountryService countryService) {
        this.countryService = countryService;
    }
}